<?php
header('Content-Type: text/html; charset=utf-8');

function GetServidor($servidor)
{
     require "conexao1.inc.php";
     $sql = sprintf("SELECT db,usuario,senha,diretorio,titulo FROM Tables WHERE servidor='$servidor'");
     $result = mysql_query($sql, $dbctl);
     if($result == 0)
     {
          printf("Falha: %s\n", mysql_error($dbctl));
          printf("%s\n", $sql);
          exit();
     }
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               $_REQUEST['Server'] = $servidor;
               $_REQUEST['User'] = $myrow[1];
               $_REQUEST['Pwd'] = $myrow[2];
               $_REQUEST['DB'] = $myrow[0];
               $_REQUEST['DIRETORIO'] = $myrow[3];
               $_REQUEST['Title'] = $myrow[4];
          }
     }
     mysql_close($dbctl);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
     <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <meta http-equiv="Pragma" content="no-cache" />
          <meta http-equiv="Expires" content="-1" />
          <title>Builder</title>
          <script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
          <script type="text/javascript">
               $(document).ready(function() {

                    $('#APELIDO').change(function() {
                         url = "index.php?AP=" + $('#APELIDO').val();
                         location.href=url;
                    });
               });
                                   
          </script>
     </head>
     <body>
          <?php
          if(isset($_REQUEST['Build']) || isset($_REQUEST['UnBuild']) || isset($_REQUEST['Montagem']))
          {?>
          <h2 align='center'><a href='./'>Nova Gera&ccedil;&atilde;o</a></h2>
          <?php }
          //phpinfo();
          //exit;
          include "function.inc.php";
          include "PrepLink.inc.php";
          if(isset($_REQUEST['Montagem']))
          {
               GetServidor($_REQUEST['APELIDO']);
               $dbctl = mysql_connect($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB']);
               if(!$dbctl)
               {
                    printf("<p>Falha na conexão com Base de Controle</p>");
                    include "Form.inc.php";
                    exit();
               }
               mysql_select_db($_REQUEST['DB'], $dbctl);
               CreateMontagem($dbctl);
               $nTables = 0;
               $TABLES = array();
               $TITULOS = array();
               //$sql = "DELETE FROM Page_mount";
               //$result = mysql_query($sql, $dbctl);

               $sql = "DELETE FROM Menu_mount WHERE automatico='1'";
               $result = mysql_query($sql, $dbctl);

               $sql = "OPTIMIZE TABLE Menu_mount";
               $result = mysql_query($sql, $dbctl);

               $sql = "ALTER TABLE Menu_mount AUTO_INCREMENT=1";
               $result = mysql_query($sql, $dbctl);

               mountLink($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB']);
               $nTables = getTables($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB'], $TABLES, $TITULOS);
               for($nTable = 0; $nTable < $nTables; $nTable++)
               {
                    getTableInfo($dbctl, $_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB'], $TABLES[$nTable]);
                    printf("<h3 align='center'>Tabela '%s' Montada com Sucesso</h3>\n", ucfirst($TABLES[$nTable]));
               }
          }
          else if(isset($_REQUEST['UnBuild']))
          {
               GetServidor($_REQUEST['APELIDO']);
               $dbctl = mysql_connect($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB']);
               if(!$dbctl)
               {
                    printf("<p>Falha na conexão com Base de Controle</p>");
                    exit();
               }
               mysql_select_db($_REQUEST['DB'], $dbctl);

               if(isset($_REQUEST['Title']))
               {
                    if($_REQUEST['Title'] == "")
                    {
                         unset($_REQUEST['Title']);
                    }
               }
               if(isset($_REQUEST['Header']))
               {
                    if($_REQUEST['Header'] == "")
                    {
                         unset($_REQUEST['Header']);
                    }
               }
               if(isset($_REQUEST['Session']))
               {
                    if($_REQUEST['Session'] == "")
                    {
                         unset($_REQUEST['Session']);
                    }
               }
               if(isset($_REQUEST['Dir']))
               {
                    if($_REQUEST['Dir'] == "")
                    {
                         unset($_REQUEST['Dir']);
                    }
               }
               $db = mysql_connect($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd']);
               if(!$db)
               {
                    printf("<h1 align=center>Falha no conexao com a Base de Dados</h1>\n");
                    exit;
               }
               mysql_select_db($_REQUEST['DB'], $db);
               mysql_query("SET NAMES 'utf8'");
               mysql_query("SET character_set_connection=utf8");
               mysql_query("SET character_set_client=utf8");
               mysql_query("SET character_set_results=utf8");
               $diretorio = "../" . strtolower($_REQUEST['DIRETORIO']);
               if(!is_dir($diretorio))
               {
                    printf("<p>Instalação ja não existe<p>");
                    exit();
               }
               $nTables = 0;
               $TABLES = array();
               $TITULOS = array();
               $nTables = getTables($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB'], $TABLES, $TITULOS);
               for($nTable = 0; $nTable < $nTables; $nTable++)
               {
                    $TABLE = $TABLES[$nTable];
                    $TITULO = $TITULOS[$nTable];

                    $sqlF = sprintf("SELECT script FROM Page_mount WHERE Tabela='%s' GROUP BY script ORDER BY script", $TABLE);
                    $resultF = mysql_query($sqlF, $dbctl);
                    if($resultF == 0)
                    {
                         printf("<h1>(index-1)Falha: %s</h1>\n", mysql_error($dbctl));
                         printf("<h3>%s</h3>\n", $sql);
                         exit;
                    }
                    else
                    {
                         if(mysql_num_rows($resultF) > 0)
                         {
                              while($myrowF = mysql_fetch_row($resultF))
                              {

                                   //Nome do Arquivo PHP
                                   $FILE = sprintf("%s/Cad%s.php", $diretorio, ucfirst($myrowF[0]));
                                   if(file_exists($FILE))
                                   {
                                        unlink($FILE);
                                   }

                                   $dir = sprintf("%s/my_js", $diretorio);
                                   $my_js = sprintf("%s/Cad%s.js", $dir, ucfirst($myrowF[0]));
                                   if(file_exists($my_js))
                                   {
                                        unlink($my_js);
                                   }

                                   $dir = sprintf("%s/my_ajax", $diretorio);
                                   $my_ajax = sprintf("%s/Cad%s_ajax.php", $dir, ucfirst($myrowF[0]));
                                   if(file_exists($my_ajax))
                                   {
                                        unlink($my_ajax);
                                   }

                                   $dir = sprintf("%s/my_inc", $diretorio);
                                   $my_inc = sprintf("%s/Cad%s.inc.php", $dir, ucfirst($myrowF[0]));
                                   if(file_exists($my_inc))
                                   {
                                        unlink($my_inc);
                                   }

                                   printf("<h3 align='center'>Arquivo 'Cad%s.php' Removido com Sucesso</h3>\n", ucfirst($myrowF[0]));
                              }
                         }
                    }
               }
               $FILE = sprintf("%s/index.php", $diretorio);
               if(file_exists($FILE))
               {
                    unlink($FILE);
               }
               printf("<h3 align='center'>Arquivo 'index.php' Removido com Sucesso</h3>\n");

               $FILE = sprintf("%s/conexao.inc.php", $diretorio);
               if(file_exists($FILE))
               {
                    unlink($FILE);
               }
               printf("<h3 align='center'>Arquivo 'conexao.inc.php' Removido com Sucesso</h3>\n");

               $FILE = sprintf("%s/conexaoTMP.inc.php", $diretorio);
               if(file_exists($FILE))
               {
                    unlink($FILE);
               }
               printf("<h3 align='center'>Arquivo 'conexaoTMP.inc.php' Removido com Sucesso</h3>\n");

               $FILE = sprintf("%s/define.php", $diretorio);
               if(file_exists($FILE))
               {
                    unlink($FILE);
               }
               printf("<h3 align='center'>Arquivo 'define.php' Removido com Sucesso</h3>\n");

               $FILE = sprintf("%s/login.php", $diretorio);
               if(file_exists($FILE))
               {
                    unlink($FILE);
               }
               printf("<h3 align='center'>Arquivo 'login.php' Removido com Sucesso</h3>\n");

               $FILE = sprintf("%s/includes/Menu.inc.php", $diretorio);
               if(file_exists($FILE))
               {
                    unlink($FILE);
               }
               printf("<h3 align='center'>Arquivo 'Menu.inc.php' Removido com Sucesso</h3>\n");

               mysql_close($db);
               mysql_close($dbctl);
          }
          else if(isset($_REQUEST['Build']))
          {
               GetServidor($_REQUEST['APELIDO']);
               $dbctl = mysql_connect($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB']);
               if(!$dbctl)
               {
                    printf("<p>Falha na conexão com Base de Controle</p>");
                    exit();
               }
               mysql_select_db($_REQUEST['DB'], $dbctl);

               if(isset($_REQUEST['Title']))
               {
                    if($_REQUEST['Title'] == "")
                    {
                         unset($_REQUEST['Title']);
                    }
               }
               if(isset($_REQUEST['Header']))
               {
                    if($_REQUEST['Header'] == "")
                    {
                         unset($_REQUEST['Header']);
                    }
               }
               if(isset($_REQUEST['Session']))
               {
                    if($_REQUEST['Session'] == "")
                    {
                         unset($_REQUEST['Session']);
                    }
               }
               if(isset($_REQUEST['Dir']))
               {
                    if($_REQUEST['Dir'] == "")
                    {
                         unset($_REQUEST['Dir']);
                    }
               }
               $db = mysql_connect($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd']);
               if(!$db)
               {
                    printf("<h1 align=center>Falha no conexao com a Base de Dados</h1>\n");
                    exit;
               }
               mysql_select_db($_REQUEST['DB'], $db);
               mysql_query("SET NAMES 'utf8'");
               mysql_query("SET character_set_connection=utf8");
               mysql_query("SET character_set_client=utf8");
               mysql_query("SET character_set_results=utf8");
               $diretorio = "../" . strtolower($_REQUEST['DIRETORIO']);
               if(!is_dir($diretorio))
               {
                    mkdir($diretorio);
               }
               mount_js_link($db);
               $nTables = 0;
               $TABLES = array();
               $TITULOS = array();
               $nTables = getTables($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB'], $TABLES, $TITULOS);
               //if($nTables > 1)
               // {
               if(isset($_REQUEST['Dir']))
               {
                    GeraIndex($dbctl, $db, $diretorio, $nTables, $TABLES, "1");
               }
               else
               {
                    GeraIndex($dbctl, $db, $diretorio, $nTables, $TABLES, "0");
               }
               //}
               for($nTable = 0; $nTable < $nTables; $nTable++)
               {
                    $TABLE = $TABLES[$nTable];
                    $TITULO = $TITULOS[$nTable];
                    //GetInfo($_REQUEST['Server'], $_REQUEST['DB'], $TABLE, $SHOW, $INPUT, $RELATORIOS);
                    //$nCampos = 0;
                    require "PrepFunc.inc.php";
                    require "PrepAjax.inc.php";

                    $sqlF = sprintf("SELECT script FROM Page_mount WHERE Tabela='%s' GROUP BY script ORDER BY script", $TABLE);
                    $resultF = mysql_query($sqlF, $dbctl);
                    if($resultF == 0)
                    {
                         printf("<h1>(index-1)Falha: %s</h1>\n", mysql_error($dbctl));
                         printf("<h3>%s</h3>\n", $sql);
                         exit;
                    }
                    else
                    {
                         if(mysql_num_rows($resultF) > 0)
                         {
                              while($myrowF = mysql_fetch_row($resultF))
                              {
                                   $SCRIPT = $myrowF[0];

                                   $out = sprintf("<?php\n");

                                   if(isset($_REQUEST['ISO']))
                                   {
                                        $out .= sprintf("header(\"Content-type: text/html; charset=iso-8859-1\");\n");
                                   }
                                   else if(isset($_REQUEST['UTF8']))
                                   {
                                        //$out .= sprintf("header(\"Content-type: text/html; charset=utf-8\");\n");
                                   }
                                   //$out .= sprintf("header(\"Cache-Control: no-cache, must-revalidate\");\n");
                                   if(isset($_REQUEST['Session']))
                                   {
                                        $out .= sprintf("session_start();\n");
                                        $out .= sprintf("if(!isset(\$_SESSION['%s']))\n", $_REQUEST['Session']);
                                        $out .= sprintf("{\n");
                                        $out .= sprintf("     Header(\"Location: login.php\");\n");
                                        $out .= sprintf("     exit;\n");
                                        $out .= sprintf("}\n");
                                   }
                                   if(isset($_REQUEST['Dir']))
                                   {
                                        //Inserir_Direitos($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB'], $TABLE);
                                        //Inserir_Direitos($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB'], $myrowF[0]);
                                        $out .= sprintf("require \"define.php\";\n");
                                        //$out .= sprintf("if(!isset(\$DIR_EDITAR_%s) && !isset(\$DIR_LISTAR_%s))\n", strtoupper($TABLE), strtoupper($TABLE));
                                        $out .= GetDir($dbctl, $myrowF[0]);
                                        //$out .= sprintf("if(!isset(\$DIR_EDITAR_%s) && !isset(\$DIR_LISTAR_%s))\n", strtoupper($myrowF[0]), strtoupper($myrowF[0]));
                                        $out .= sprintf("{\n");
                                        $out .= sprintf("     Header(\"Location: index.php\");\n");
                                        $out .= sprintf("     exit;\n");
                                        $out .= sprintf("}\n");
                                   }

                                   //$out .= sprintf("\$filename = \"./my_inc/Cad%s.inc.php\";\n", ucfirst($TABLE));
                                   $out .= sprintf("\$filename = \"./my_inc/Cad%s.inc.php\";\n", ucfirst($myrowF[0]));
                                   $out .= "if(file_exists(\$filename))\n";
                                   $out .= "{\n";
                                   //$out .= sprintf("     include \"./my_inc/Cad%s.inc.php\";\n", ucfirst($TABLE));
                                   $out .= sprintf("     include \"./my_inc/Cad%s.inc.php\";\n", ucfirst($myrowF[0]));
                                   $out .= "}\n";
                                   $out .= "?>\n";

                                   $out .= sprintf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
                                   $out .= sprintf("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='pt-br' lang='pt-br' >\n");
                                   $out .= sprintf("<head>\n");
                                   if(isset($_REQUEST['ISO']))
                                   {
                                        $out .= sprintf("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n");
                                   }
                                   else if(isset($_REQUEST['UTF8']))
                                   {
                                        $out .= sprintf("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n");
                                   }
                                   $out .= sprintf("<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n");
                                   $out .= sprintf("<meta http-equiv=\"Expires\" content=\"-1\" />\n");
                                   if(isset($_REQUEST['Title']))
                                   {
                                        $out .= sprintf("<title>%s</title>\n", $_REQUEST['Title']);
                                   }
                                   else
                                   {
                                        //$out .= sprintf("<title>Cadastro de %s</title>\n", ucfirst($TABLE));
                                        $out .= sprintf("<title>Cadastro de %s</title>\n", ucfirst($myrowF[0]));
                                   }

                                   /*
                                    * Inicio da geração dos menus
                                    */
                                   include "PrepMenu.inc.php";

                                   $out .= sprintf("     <br />\n");

                                   /*
                                    * Iniciando scripts
                                    */
                                   $out .= sprintf("     <?php\n");
                                   $out .= sprintf("     require \"conexao.inc.php\";\n");

                                   /*
                                    * Inclusao de funcoes especificas
                                    * definida pelo usuario
                                    */
                                   //$tmp = sprintf("%s/includes/Cad%s_func.inc.php", $diretorio, ucfirst($TABLE));
                                   $tmp = sprintf("%s/includes/Cad%s_func.inc.php", $diretorio, ucfirst($myrowF[0]));
                                   if(is_file($tmp))
                                   {
                                        //$tmp = sprintf("includes/Cad%s_func.inc.php", ucfirst($TABLE));
                                        $tmp = sprintf("includes/Cad%s_func.inc.php", ucfirst($myrowF[0]));
                                        $out .= sprintf("     require \"%s\";\n", $tmp);
                                   }
                                   $out .= sprintf("     ?>\n");

                                   /*
                                    * Inicio da geração da parte de Inclusão de dados
                                    * na Base escolhida
                                    * Funcao "Incluir"
                                    */
                                   include "PrepIncluir.inc.php";

                                   /*
                                    * Inicio da geração da parte de Listagem no Dialogo
                                    */
                                   include "PrepLista.inc.php";

                                   /*
                                    * Finalizando Geração do Arquivo
                                    */
                                   $out .= sprintf("<?php\n");
                                   $out .= sprintf("mysql_close(\$db);\n");
                                   $out .= sprintf("?>\n");
                                   if($_REQUEST['Menu'] == 'Novo')
                                   {
                                        $out .= sprintf("     <script type='text/javascript'> \n");
                                        $out .= sprintf("          var menu=new menu.dd('menu');\n");
                                        $out .= sprintf("          menu.init('menu','menuhover');\n");
                                        $out .= sprintf("     </script>\n");
                                   }

                                   $out .= sprintf("<div id='dialog-info' title='Informações'>\n");
                                   $out .= sprintf("     <p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span><div id='info'></div></p>\n");
                                   $out .= sprintf("</div>\n");
                                   $out .= sprintf("<form method='post' name='frmexcel' id='frmexcel' action='csvExport.php' target='_blank'>\n");
                                   $out .= sprintf("     <input type='hidden' name='csvBuffer' id='csvBuffer' value='' />\n");
                                   $out .= sprintf("</form>\n");
                                   $out .= sprintf("<div id=\"loading\" style=\"display:none\">\n");
                                   $out .= sprintf("<img src=\"img/loading.gif\" alt=\"Loader\" />\n");
                                   $out .= sprintf("</div>\n");
                                   if(isset($_REQUEST['Aviso']))
                                   {
                                        $out .= AvisoPHP();
                                   }
                                   $out .= sprintf("</body>\n");
                                   $out .= sprintf("</html>\n");

                                   //Nome do Arquivo PHP
                                   //$FILE = sprintf("%s/Cad%s.php", $diretorio, ucfirst($TABLE));
                                   $FILE = sprintf("%s/Cad%s.php", $diretorio, ucfirst($myrowF[0]));
                                   $fPHP = fopen($FILE, "w");
                                   if(!$fPHP)
                                   {
                                        echo "<h1>Falha na Criacao do Arquivo '" . $FILE . "'</h1>";
                                        exit;
                                   }
                                   fprintf($fPHP, "%s", $out);
                                   fclose($fPHP);

                                   //require "PrepFunc.inc.php";
                                   //require "PrepAjax.inc.php";

                                   $js .= "});\n";
                                   $dir = sprintf("%s/my_js", $diretorio);
                                   if(!is_dir($dir))
                                   {
                                        mkdir($dir);
                                   }
                                   //$my_js = sprintf("%s/Cad%s.js", $dir, ucfirst($TABLE));
                                   $my_js = sprintf("%s/Cad%s.js", $dir, ucfirst($myrowF[0]));
                                   $fd = fopen($my_js, "w");
                                   if($fd)
                                   {
                                        fprintf($fd, "%s", $js);
                                        fclose($fd);
                                   }
                                   printf("<h3 align='center'>Arquivo 'Cad%s.php' Gerado com Sucesso</h3>\n", ucfirst($myrowF[0]));
                              }
                         }
                    }
               }
               // Criar conexao.inc.php
               $FILE = sprintf("%s/conexao.inc.php", $diretorio);
               $fPHP = fopen($FILE, "w");
               if(!$fPHP)
               {
                    echo "<h1>Falha na Criacao do Arquivo '" . $FILE . "'</h1>";
                    exit;
               }
               fprintf($fPHP, "<?php\n");
               fprintf($fPHP, "\$db = mysql_connect('%s','%s','%s');\n", $_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd']);
               fprintf($fPHP, "mysql_select_db('%s');\n", $_REQUEST['DB']);
               if(isset($_REQUEST['UTF8']))
               {
                    fprintf($fPHP, "mysql_query(\"SET NAMES 'utf8'\");\n");
                    fprintf($fPHP, "mysql_query(\"SET character_set_connection=utf8\");\n");
                    fprintf($fPHP, "mysql_query(\"SET character_set_client=utf8\");\n");
                    fprintf($fPHP, "mysql_query(\"SET character_set_results=utf8\");\n");
               }
               fprintf($fPHP, "?>\n");
               fclose($fPHP);
               printf("<h3 align='center'>Arquivo 'conexao.inc.php' Gerado com Sucesso</h3>\n");

               // Criar conexaoTMP.inc.php
               $FILE = sprintf("%s/conexaoTMP.inc.php", $diretorio);
               $fPHP = fopen($FILE, "w");
               if(!$fPHP)
               {
                    echo "<h1>Falha na Criacao do Arquivo '" . $FILE . "'</h1>";
                    exit;
               }
               fprintf($fPHP, "<?php\n");
               fprintf($fPHP, "\$dbtmp = mysql_connect('%s','%s','%s');\n", $_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd']);
               fprintf($fPHP, "mysql_select_db('%s');\n", $_REQUEST['DB']);
               if(isset($_REQUEST['UTF8']))
               {
                    fprintf($fPHP, "mysql_query(\"SET NAMES 'utf8'\");\n");
                    fprintf($fPHP, "mysql_query(\"SET character_set_connection=utf8\");\n");
                    fprintf($fPHP, "mysql_query(\"SET character_set_client=utf8\");\n");
                    fprintf($fPHP, "mysql_query(\"SET character_set_results=utf8\");\n");
               }
               fprintf($fPHP, "?>\n");
               fclose($fPHP);
               printf("<h3 align='center'>Arquivo 'conexaoTMP.inc.php' Gerado com Sucesso</h3>\n");

               // Criar define.php
               $FILE = sprintf("%s/define.php", $diretorio);
               $fPHP = fopen($FILE, "w");
               if(!$fPHP)
               {
                    echo "<h1>Falha na Criacao do Arquivo '" . $FILE . "'</h1>";
                    exit;
               }
               fprintf($fPHP, "<?php\n");
               fprintf($fPHP, "\$TEMA = 'css/smoothness/jquery-ui-1.8.16.custom.css';\n");
               fprintf($fPHP, "require 'conexao.inc.php';\n");
               fprintf($fPHP, "if(\$db)\n");
               fprintf($fPHP, "{\n");
               fprintf($fPHP, "     \$sql = sprintf(\"SELECT Direito FROM Direitos WHERE idUsuario='%%s' ORDER BY Direito\",\$_SESSION['ID']);\n");
               fprintf($fPHP, "     \$result = mysql_query(\$sql,\$db);\n");
               fprintf($fPHP, "     if(\$result)\n");
               fprintf($fPHP, "     {\n");
               fprintf($fPHP, "          while(\$myrow = mysql_fetch_row(\$result))\n");
               fprintf($fPHP, "          {\n");
               fprintf($fPHP, "               \$var = \$myrow[0];\n");
               fprintf($fPHP, "               \$\$var = 1;\n");
               fprintf($fPHP, "          }\n");
               fprintf($fPHP, "     }\n");
               fprintf($fPHP, "     \$sql = sprintf(\"SELECT tema FROM Usuario WHERE idUsuario='%%s'\",\$_SESSION['ID']);\n");
               fprintf($fPHP, "     \$result = mysql_query(\$sql,\$db);\n");
               fprintf($fPHP, "     if(\$result)\n");
               fprintf($fPHP, "     {\n");
               fprintf($fPHP, "          while(\$myrow = mysql_fetch_row(\$result))\n");
               fprintf($fPHP, "          {\n");
               fprintf($fPHP, "               \$TEMA = \$myrow[0];\n");
               fprintf($fPHP, "          }\n");
               fprintf($fPHP, "     }\n");
               fprintf($fPHP, "     mysql_close(\$db);\n");
               fprintf($fPHP, "}\n");
               fprintf($fPHP, "?>\n");
               printf("<h3 align='center'>Arquivo 'define.php' Gerado com Sucesso</h3>\n");

               require "PrepLogin.inc.php";
               printf("<h3 align='center'>Arquivo 'login.php' Gerado com Sucesso</h3>\n");

               require "PrepExtras.inc.php";

               mysql_close($db);
               mysql_close($dbctl);
          }
          else
          {
               include "Form.inc.php";
          }
          ?>
          <h2 align='center'><a href='./'>Nova Gera&ccedil;&atilde;o</a></h2>
     </body>
</html>
